package com.weiyun.shortlink.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiyun.shortlink.admin.common.convention.result.Result;
import com.weiyun.shortlink.admin.common.convention.result.Results;
import com.weiyun.shortlink.admin.dto.req.ShortLinkStatsReqDTO;
import com.weiyun.shortlink.admin.dto.resp.ShortLinkStatsRespDTO;
import com.weiyun.shortlink.admin.remote.ShortLinkActualRemoteService;
import com.weiyun.shortlink.admin.remote.ShortLinkRemoteService;
import com.weiyun.shortlink.admin.remote.dto.req.*;
import com.weiyun.shortlink.admin.remote.dto.resp.*;
import com.weiyun.shortlink.admin.toolkit.EasyExcelWebUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 *   @Author:weiyun
 *
 */
@RestController
@RequiredArgsConstructor
public class ShortLinkController {
    ShortLinkRemoteService shortLinkRemoteService = new ShortLinkRemoteService() {
    };

    private final ShortLinkActualRemoteService shortLinkActualRemoteService;
    @GetMapping("/api/shortLink/admin/v1/page")
    public Result<Page<ShortLinkPageRespDto>> pageShortLink(ShortLinkPageReqDto requestParam) {
        return shortLinkActualRemoteService.pageShortLink(requestParam.getGid(),requestParam.getOrderTag(),requestParam.getCurrent(),requestParam.getSize());
    }

    @PostMapping("/api/shortLink/admin/v1/create")
    public Result<ShortLinkCreateRespDto> createShortLink(@RequestBody ShortLinkCreateReqDto shortLinkCreateReqDto) throws Exception {

        return shortLinkActualRemoteService.createShortLink(shortLinkCreateReqDto);
    }

    @PostMapping("api/shortLink/admin/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDto shortLinkUpdateReqDto) {
        shortLinkActualRemoteService.updateShortLink(shortLinkUpdateReqDto);
        return Results.success();
    }

    @GetMapping("/api/shortLink/admin/v1/stats")
    public Result<ShortLinkStatsRespDTO> shortLinkStats(ShortLinkStatsReqDTO requestParam) {

//        return Results.success(shortLinkActualRemoteService.oneShortLinkStats(requestParam.getFullShortUrl(),requestParam.getGid(),requestParam.getStartDate(),requestParam.getEndDate()));
        return Results.success(shortLinkActualRemoteService.oneShortLinkStats(requestParam.getFullShortUrl(),requestParam.getGid(),requestParam.getStartDate(),requestParam.getEndDate()).getData());

    }

    @GetMapping("/api/shortLink/admin/v1/stats/access-record")
    public Result<Page<ShortLinkStatsAccessRecordRespDTO>> shortLinkStatsRecordAccess(ShortLinkAccessRecordReqDTO requestParam) {
        return shortLinkActualRemoteService.shortLinkStatsRecordAccess(requestParam.getFullShortUrl(),requestParam.getGid(),requestParam.getStartDate(),requestParam.getEndDate());
    }

    /**
     * 访问分组短链接指定时间内监控数据
     */
    @GetMapping("/api/shortLink/admin/v1/stats/group")
    public Result<ShortLinkStatsRespDTO> groupShortLinkStats(ShortLinkGroupStatsReqDTO requestParam) {

        return shortLinkActualRemoteService.groupShortLinkStats(requestParam.getGid(),requestParam.getStartDate(),requestParam.getEndDate(),requestParam.getFullShortUrl());
    }
    @GetMapping("/api/shortLink/admin/v1/stats/accessRecord/group")
    public Result<Page<ShortLinkStatsAccessRecordRespDTO>> groupShortLinkStatsAccessRecord(ShortLinkGroupStatsAccessRecordReqDto requestParam) {
        return shortLinkActualRemoteService.groupShortLinkStatsAccessRecord(requestParam.getGid(),requestParam.getStartDate(),requestParam.getEndDate());
    }

    /**
     * 批量创建短链接
     */
    @SneakyThrows
    @PostMapping("/api/shortLink/admin/v1/create/batch")
    public void batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO requestParam, HttpServletResponse response) {
        Result<ShortLinkBatchCreateRespDTO> shortLinkBatchCreateRespDTOResult = shortLinkActualRemoteService.batchCreateShortLink(requestParam);
        if (shortLinkBatchCreateRespDTOResult.isSuccess()) {
            List<ShortLinkBaseInfoRespDTO> baseLinkInfos = shortLinkBatchCreateRespDTOResult.getData().getBaseLinkInfos();
            EasyExcelWebUtil.write(response, "批量创建短链接-SaaS短链接系统", ShortLinkBaseInfoRespDTO.class, baseLinkInfos);
        }
    }
}